package com.czxy.service;

import com.czxy.mapper.*;
import com.czxy.pojo.Sku;
import com.czxy.pojo.SkuPhoto;
import com.czxy.pojo.Spec;
import com.czxy.pojo.Spu;
import com.czxy.vo.OneSkuResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 张青卯
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class SkuService {

    private final CategoryMapper categoryMapper;

    private final SkuPhotoMapper skuPhotoMapper;

    private final SpecMapper specificationMapper;

    private final SkuCommentMapper skuCommentMapper;

    private final SpuMapper spuMapper;

    private final SkuMapper skuMapper;

    @Autowired
    public SkuService(SkuMapper skuMapper, SpuMapper spuMapper, SkuCommentMapper skuCommentMapper, CategoryMapper categoryMapper, SkuPhotoMapper skuPhotoMapper, SpecMapper specificationMapper) {
        this.skuMapper = skuMapper;
        this.spuMapper = spuMapper;
        this.skuCommentMapper = skuCommentMapper;
        this.categoryMapper = categoryMapper;
        this.skuPhotoMapper = skuPhotoMapper;
        this.specificationMapper = specificationMapper;
    }

    public OneSkuResult findSkuBySkuId(Integer skuId){
        OneSkuResult result = new OneSkuResult();
        Sku sku = skuMapper.selectByPrimaryKey(skuId);
        Spu spu = spuMapper.selectByPrimaryKey(sku.getSpu_id());
        result.setSkuid(sku.getId());
        result.setSpuid(spu.getId());
        result.setGoods_name(sku.getSku_name());
        result.setPrice(sku.getPrice());
        result.setOn_sale_date(spu.getOn_sale_time());
        Integer count = skuCommentMapper.findCountBySpuId(spu.getId());
        result.setComment_count(count);
        Double level = skuCommentMapper.findAvgStarBySpuId(spu.getId());
        double v = Math.floor(level);
        int i = (int)v;
        result.setComment_level(i);
        result.setCat1_info(categoryMapper.selectByPrimaryKey(spu.getCat1_id()));
        result.setCat2_info(categoryMapper.selectByPrimaryKey(spu.getCat2_id()));
        result.setCat3_info(categoryMapper.selectByPrimaryKey(spu.getCat3_id()));
        HashMap<String, String> map = new HashMap<>(3);
        map.put("smlogo",sku.getImages());
        map.put("biglogo",sku.getImages());
        map.put("xbiglogo",sku.getImages());
        result.setLogo(map);
        List<SkuPhoto> photos = skuPhotoMapper.findSkuPhotoBySkuId(sku.getId());
        ArrayList<Map<String,String>> photoList = new ArrayList<>();
        for (SkuPhoto sp : photos) {
            HashMap<String, String> hashMap = new HashMap<>(3);
            hashMap.put("smimg",sp.getUrl());
            hashMap.put("bigimg",sp.getUrl());
            hashMap.put("xbigimg",sp.getUrl());
            photoList.add(hashMap);
        }
        result.setPhotos(photoList);
        result.setDescription(spu.getDescription());
        result.setAftersale(spu.getAftersale());
        result.setStock(sku.getStock());
        String[] spec_list_array = sku.getSpec_list().split("\\|");
        ArrayList<Integer> idList = new ArrayList<>();
        for (String s : spec_list_array) {
            String[] ids = s.split(":");
            idList.add(Integer.parseInt(ids[0]));
        }
        ArrayList<Spec> list = new ArrayList<>();
        for (Integer id : idList) {
            list.add(specificationMapper.findSpecificationBySpecid(id));
        }
        result.setSpec_list(list);
        Map<String,String> spec_info = new HashMap<>(3);
        spec_info.put("id_list",sku.getSpec_list());
        spec_info.put("id_txt",sku.getSpec_list_code().replace("{","".replace("}","")));
        result.setSpec_info(spec_info);
        List<Sku> skuList = skuMapper.findSkuListBySpuId(sku.getSpu_id());
        List<Map> sku_list = new ArrayList<>();
        for (Sku s : skuList) {
            HashMap<Object, Object> obj = new HashMap<>(1);
            obj.put("skuid",s.getId());
            obj.put("id_list",s.getSpec_list().replace("{","").replace("}",""));
            sku_list.add(obj);
        }
        result.setSku_list(sku_list);
        return result;
    }
}
